<?php


/*
 *  Recorre blogs agafant títols i enllaços
 *  d'entrades i enllaços cap a altres
 *  blogs
 *
*/

//error_reporting(0);

function __autoload($class) {
	    
    global $CONFIG;
    require_once($CONFIG->datadir.'/bot/lib/'.$class.'.php');
}


require_once('../config.php');
require_once($CONFIG->datadir.'/lib/db/DB.php');


/////////////////////////////////////////////


header('Content-Type: text/html; charset=UTF-8');

$timestart = time();

$DB = new DB();


$system = new System();
$langs = $system->getSystemLangs();
$links = $system->getPendingLinks();

$i = 0;
while($i < $CONFIG->Npetitions){

    if(empty($links[$i])){
        $links = $system->loadPendingLinks($i);
    }

    unset($blog);
    $blog = new Blog($links[$i]['id']);
    
    // We want a final /
    $links[$i]['url'] = rtrim($links[$i]['url'], '/').'/';

    
    $allentriesurl = $links[$i]['url'].'search?max-results='.$CONFIG->entriesperblog;

    if ($blog->getBlogHtml($allentriesurl)) {
    	
        $blog->saveLinks($links[$i]['name']);
        //$head = getmetas($code);
        
        $blog->domClean();

        // Last entry lang saved to speed up next entry language detection
        $lastlang = $blog->getEntries($lastlang, $langs, $head);
    }

    //$blog->Visited($links[$i]);
    $blog->Visited();

    
    if ($CONFIG->sleep) {
        sleep($CONFIG->sleep);
    }
    
    $i = $i + 1;
}


$timeend = time();
echo date('i:s', $timeend-$timestart).' mins:secs<br>';

echo '<br>Petitions: '.$CONFIG->Npetitions;
echo '<br>Next link: '.$links[$i][0].'<br><br>';

?>
